Explicit construction of entity type [MyClass] in query is not allowed.

Posted by Code Sherpa on Stack Overflow See other posts from Stack Overflow or by Code Sherpa
Published on 2010-06-01T19:55:50Z Indexed on 2010/06/02 3:33 UTC
Read the original article Hit count: 1031

Filed under:
|
|

Hi. Like the title says, I have the following exception:

Description: Event code: 3005 Event message: An unhandled exception has occurred. Exception information: Exception type: NotSupportedException Exception message: Explicit construction of entity type 'Company.Project.Core.Domain.Friend' in query is not allowed.

I am using LINQ and have the following code in my datacontext:

var friends2 = (dc.Friends
                        .Where(f => f.MyFriendsAccountId == 
                            accountId && f.AccountId != accountId)
                        .Select(f => new
                                {
                                    f.FriendId,
                                    AccountId = f.MyFriendsAccountId,
                                    MyFriendsAccountId = f.AccountId, 
                                    f.CreateDate, f.Timestamp
                                })).Distinct();

                        result.AddRange(friends2
                            .Select(o => new Friend
                                 {
                                     FriendId = o.FriendId, 
                                     AccountId = o.AccountId, 
                                     CreateDate = o.CreateDate, 
                                     MyFriendsAccountId = o.MyFriendsAccountId, 
                                     Timestamp = o.Timestamp
                                 }));

the final code block is throwing the error and I am pretty sure it is this statement that is the culprit:

.Select( o => **new Friend**

How should I be reworking my code to avoid this error? Code illustration appreciated.

Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET